<?php 
/****
 * 基本event模型, 主要用于insert\update操作. 而select在多数情况下需要额外信息, 一般用Model.Extend包中的扩展model替代. 
 */
class EventModel extends Model { 
    static private $_CONST=array(
        'STATE_UNKNOWN'=> -1,
        'STATE_HIDDEN'=> 1,
        'STATE_NORMAL'=> 0,
        'STATE_LOCKED'=> 4,
        'LOGIN_ERROR_PASSWORD'=> -2,
        'LOGIN_ERROR_ANSWER'=> -3,
    );
    static public function C($name){
        return self::$_CONST[$name];
    }
    /****
    * 附属物，获取所有活动类型
    */
    static public function getTypes(){
        return L('EVENT_TYPE');
    }
    // 自动验证设置 
    protected $_validate    =      array( 
        array('id', 'require', "错误的ID", Model::VALUE_VAILIDATE ),
        array('title','require',"请输入标题",Model::MUST_VALIDATE ), 
        array('type','require',"请选择类型"), 
        array('content','require',"请输入内容", Model::MUST_VALIDATE ), 
        //array('title','','标题已经存在',0,'unique',self::MODEL_INSERT), 
        ); 
    // 自动填充设置 
    protected $_auto    =      array( 
        array('stamp_created','date',self::MODEL_INSERT,'function',DATE_ATOM), 
        array('stamp_modified','time',self::MODEL_BOTH,'function'), 
        array('visibility', 'checkState', Model::MODEL_BOTH,'callback'),
        );
    public function checkState($state){
        if (self::C('STATE_HIDDEN')==$state ||
            self::C('STATE_LOCKED')==$state || 
            self::C('STATE_NORMAL')==$state){
            return $state;
        } else{
            return self::C('STATE_NORMAL');
        }
    }
    public function selectSimple($options=array()){
        if (!$this->options['order']){
            $this->order('time_start DESC');
        }
        return $this->field(array('id', 'author_id', 'title', 'location', 'img', 'time_start', 'time_end', 
                                'type', 'role'))
                    ->select($options);
    }
    /****
    * 处理用户tag
    */
    public function setTag($tags, $id=null){
        if (is_null($id)){      //从当前Model获取id
            $id= $this->options['where'];
            if (is_array($id)){
                $id= $id['id'];
            }
        }
        $data= $this->tagToData($tags, $id);
        $etm= new EventTagModel();
        $map= array(
            'eid'=>$id
        );
        foreach ($data as &$v){
            $res= $etm->add($v);
            if (false===$res){
                //tag已经存在
            }
        }
        //删除原有多余tag
        $map= array(
            'eid'=>$id,
            'tag'=>array('NOT IN', $tags),
        );
        $etm->where($map)->delete();
        return $res;
    }
    /****
    * 分割字符串形式的tags
    * @return 可用于插入操作的data
    */
    protected function tagToData(&$tags, $eid){
        if (is_string($tags)){
            $tags= explode(',', $tags);
        }
        if (! is_array($tags) || count($tags) <=0){
            return false;
        }
        $data=array();
        foreach ($tags as &$v){
            $v= trim($v);
            if (strlen($v) <=0){
                continue;
            }
            $data[]= array(
                'eid'=>$eid,
                'tag'=>$v,
                'count'=>1,
            );
        }
        return $data;
    }
    public function updateTag($tags, $id=null){
        $data= $this->tagToData($tags);
        $etm= new EventTagModel();
        $map= array(
                'eid'=>$id,
                'tag'=>array('IN', $tags),
            );
        $res= $etm->where($map)->setInc('count','', 1);     //处理update状况
        if ($res < count($tags)){                           //处理不存在则insert的情况
            $r= $etm->data($data)->add();
            $res+=$r;
        }
        return $res;
    }
    protected function _after_insert($data,  $options){
        //记录TL
        TimelineModel::factor('TYPE_EVENT', $data['author_id'], $data['id'], $data['title'])->add();
    }
} 
?>